package com.rabbitmq.Config;


import com.rabbitmq.User;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;


@Slf4j
@Component
public class DeadLetterQueueConsumer {
    @RabbitListener(queues = "QD")
    public void receiveD(Message message, Channel channel) throws IOException {
//        String msg = new String(message.getBody()); //字符串获取方式
        User user ;
        try (ByteArrayInputStream bais = new ByteArrayInputStream(message.getBody());
             ObjectInputStream ois = new ObjectInputStream(bais)) {
            user = (User) ois.readObject();  // 反序列化User对象
            log.info("当前时间：{},收到死信队列信息：{}", new Date().toString(), user);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        log.info("当前时间：{},收到死信队列信息：{}", new Date().toString(), msg);
    }
}
